<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <!-- <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="审批人" prop="confTitle">
        <el-input v-model="queryParams.personIncharge" placeholder="请输入审批人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="姓名" prop="nickName">
        <el-select
          v-model="queryParams.nickName"
          placeholder="请选择"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.nickName"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- 请假状态 0未请假 1请假 -->
      <!-- <el-form-item label="请假状态" prop="offdutyStat">
        <el-input
          v-model="queryParams.offdutyStat"
          placeholder="请输入请假状态 0未请假 1请假"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="请假日期" prop="offdutyDate">
        <el-date-picker
          clearable
          v-model="queryParams.offdutyDate"
          type="date"
          placeholder="请输入请假日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:duty:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:duty:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:duty:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:duty:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dutyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <!-- <el-table-column label="用户名称" align="center" prop="userName" /> -->
      <el-table-column label="姓名" align="center" prop="nickName" />
      <!-- <el-table-column label="审批人" align="center" prop="personIncharge" />     -->
      <el-table-column label="审批人" align="center" prop="personPermit" />
      <el-table-column
        label="请假日期"
        align="center"
        prop="offdutyDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.offdutyDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="更新时间" align="center" prop="updateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="请假事由" align="center" prop="remark" />
      <el-table-column label="请假状态" align="center" prop="offdutyStat">
        <template v-slot="{ row }">
          {{ row.offdutyStat === 1 ? "审批通过" : "未审批" }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
             v-show="scope.row.userName===userName||userName==='admin'"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
         
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-check"
            @click="handleAllow(scope.row)"
            v-show="scope.row.offdutyStat !== 1"
            v-hasRole="['duty','admin']"
            >审批</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:duty:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改请假对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="请假人" prop="userName">
          <el-select
            v-model="form.userName"
            placeholder="请选择"
            @change="setNickName"
          >
            <el-option
              v-for="item in userList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="姓名" prop="nickName" >
          <el-select v-model="form.nickName"  placeholder="请选择" >
            <el-option v-for="item in userList" :key="item.userId" :label="item.nickName" :value="item.nickName">
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="请假状态 0未请假 1请假" prop="offdutyStat">
          <el-input v-model="form.offdutyStat" placeholder="请输入请假状态 0未请假 1请假" />
        </el-form-item> -->
        <el-form-item label="请假日期" prop="offdutyDate">
          <el-date-picker
            clearable
            v-model="form.offdutyDate"
            type="date"
            placeholder="请输入请假日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
          <!-- type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期" -->
        </el-form-item>
        <!-- <el-form-item label="更新时间" prop="updateDate">
          <el-date-picker clearable
            v-model="form.updateDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="删除标记 0未删除 1已删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标记 0未删除 1已删除" />
        </el-form-item> -->
        <el-form-item label="请假事由" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入请假事由" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDuty,
  getDuty,
  delDuty,
  addDuty,
  updateDuty,
} from "@/api/system/duty";
import { listUser } from "@/api/system/user.js";
export default {
  name: "Duty",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请假表格数据
      dutyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // userName: null,
        nickName: null,
        offdutyStat: null,
        offdutyDate: null,
        updateDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "请假人员不能为空", trigger: "blur" },
        ],
        offdutyDate: [
          { required: true, message: "请假日期不能为空", trigger: "blur" },
        ],
        remark: [
          { required: true, message: "请假事由不能为空", trigger: "blur" },
        ],
      },
      // 用户数组
      userList: [],
      nickName: this.$store.state.user.nickname,
      userName: this.$store.state.user.name,
    };
  },
  created() {
    console.log(this.$store.state.user);
    this.getList();
    this.getUserList();
  },
  methods: {
    /** 查询请假列表 */
    getList() {
      // const nickName = this.$store.state.user.nickname
      // const userName = this.$store.state.user.name
      if (this.userName !== "admin") {
        // console.log('不是admin账号');
        this.queryParams.nickName = this.nickName;
      }
      this.loading = true;
      listDuty(this.queryParams).then((response) => {
        this.dutyList = response.rows;
        this.total = response.total;
        this.loading = false;
        if (this.userName === "xuyongxue" || "gongchuihui") {
          this.getDutyListSpr();
        }
      });
    },
    // 根据审批人查
    getDutyListSpr() {
      const que = {
        personIncharge: this.userName,
      };
      listDuty(que).then((response) => {
        // console.log(response);
        this.dutyList = [...this.dutyList, ...response.rows];
      });
    },
    // 查询用户列表
    getUserList() {
      listUser({ pageNum: 1, pageSize: 100 }).then((res) => {
        this.userList = res.rows;
      });
    },
    // 选择请假人时，给nickname赋值中文名
    setNickName(e) {
      // console.log(e)
      listUser({ userName: e }).then((res) => {
        this.form.nickName = res.rows[0].nickName;
        this.form.personIncharge = res.rows[0].personIncharge;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        // userName: null,
        nickName: null,
        offdutyStat: null,
        offdutyDate: null,
        createTime: null,
        createBy: null,
        updateDate: null,
        updateBy: null,
        delFlag: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加请假";
      this.form.offdutyStat = 0;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDuty(id).then((response) => {
        this.form = response.data;
        console.log(this.form);
        this.form.offdutyStat=0
        // this.form.offdutyDate=this.form.offdutyDate.split(',')
        this.open = true;
        this.title = "修改请假";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // this.form.offdutyDate = this.form.offdutyDate.toString()
          // console.log(this.form);
          if (this.form.id != null) {
            updateDuty(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDuty(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除请假编号为"' + ids + '"的数据项？')
        .then(function () {
          return delDuty(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 审批按钮
    handleAllow(row) {
      // console.log(row);
      // console.log(this.nickName);
      this.$modal
        .confirm("确认同意该员工请假吗？")
        .then(async () => {
          
          return await updateDuty({ id:row.id, offdutyStat: 1,personPermit:this.nickName });
          // 
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("审批成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/duty/export",
        {
          ...this.queryParams,
        },
        `duty_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
